/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.CapacityControlEntity;
import entity.FnBBranchEntity;
import entity.TableTypeEntity;
import java.util.Date;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import exception.CapacityControlException;

@Stateful
@LocalBean
public class TableSessionBean {

    @PersistenceContext
    private EntityManager entityManager;
    String errorMessage;

    public void addTableType(long branchId, TableTypeEntity tableType) {
        entityManager.persist(tableType);
        FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
        branch.getTableType().add(tableType);
    }

    public void editTableType(TableTypeEntity tableType) {
        entityManager.merge(tableType);
    }

    public void deleteTableType(long tableTypeId, long branchId) {
        TableTypeEntity tableType = entityManager.find(TableTypeEntity.class, tableTypeId);
        FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
        branch.getTableType().remove(tableType);
        entityManager.remove(tableType);
    }

    // Retrieve all current table types of a particular F&B BranchEntity
    public List<TableTypeEntity> getAllTableType(long branchId) {
        FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
        return branch.getTableType();
    }

        public List<CapacityControlEntity> getListOfCapacityControl(long tableTypeId) {
        TableTypeEntity product = entityManager.find(TableTypeEntity.class, tableTypeId);
        return product.getCapacityControl();
    }
    public void addCapacityControl(long tableTypeId, CapacityControlEntity capacityControl) throws CapacityControlException {
        TableTypeEntity table = entityManager.find(TableTypeEntity.class, tableTypeId);
        List<CapacityControlEntity> list = table.getCapacityControl();

        if (checkConflictWithList(list, capacityControl)) {
            throw new CapacityControlException(errorMessage);
        }

        entityManager.persist(capacityControl);
        list.add(capacityControl);
        capacityControl.setTableType(table);
    }

    public void removeCapacityControl(long capacityControlId) {
        CapacityControlEntity capacity = entityManager.find(CapacityControlEntity.class, capacityControlId);
        TableTypeEntity table = capacity.getTableType();

        table.getCapacityControl().remove(capacity);
        entityManager.remove(capacity);
    }

    public void editCapacityControl(CapacityControlEntity selectedCapacityControl) throws CapacityControlException {
        List<CapacityControlEntity> list = selectedCapacityControl.getTableType().getCapacityControl();
        CapacityControlEntity temp = entityManager.find(CapacityControlEntity.class, selectedCapacityControl.getId());
        list.remove(temp);

        if (checkConflictWithList(list, selectedCapacityControl)) {
            list.add(temp);
            throw new CapacityControlException(errorMessage);
        }

        entityManager.merge(selectedCapacityControl);
        list.add(selectedCapacityControl);
    }

    private boolean checkConflictWithList(List<CapacityControlEntity> list, CapacityControlEntity capacityControl) {
        for (int i = 0; i < list.size(); i++) {
            CapacityControlEntity temp = list.get(i);
            int type1 = temp.getType();
            Date date1 = temp.getSpecificDate();
            int type2 = capacityControl.getType();
            Date date2 = capacityControl.getSpecificDate();

            if (type1 == type2 && (type1 != 8 || (type1 == 8 && compareDate(date1, date2)))) {
                errorMessage = "Time conflict with " + getInfoAboutCapacityControl(temp)
                        + ". Your new data is " + getInfoAboutCapacityControl(capacityControl);
                return true;
            }
        }
        return false;
    }

    private boolean compareDate(Date date1, Date date2) {
        return date1.getYear() == date2.getYear()
                && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate();
    }

    private String getInfoAboutCapacityControl(CapacityControlEntity temp) {
        return getType(temp.getType(), temp.getSpecificDate());
    }

    private String getType(int type, Date specificDate) {
        switch (type) {
            case 0:
                return "Default";
            case 1:
                return "Monday";
            case 2:
                return "Tuesday";
            case 3:
                return "Wednesday";
            case 4:
                return "Thursday";
            case 5:
                return "Friday";
            case 6:
                return "Saturday";
            case 7:
                return "Sunday";
            default:
                return "Date " + (specificDate.getMonth() + 1) + "/" + specificDate.getDate() + "/" + (specificDate.getYear() + 1900);
        }
    }
    
    public String removeTableType (TableTypeEntity table, long branchId) {
        TableTypeEntity tableTemp = entityManager.find(TableTypeEntity.class, table.getId());
        
        if (tableTemp.getReservedTable().isEmpty()) {
            List<CapacityControlEntity> capacityList = tableTemp.getCapacityControl();
            while (!capacityList.isEmpty()) {
                CapacityControlEntity c = capacityList.get(0);
                capacityList.remove(0);
                entityManager.remove(c);
            }
            
            FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
            branch.getTableType().remove(tableTemp);
            entityManager.merge(branch);
            
            entityManager.remove(tableTemp);            
            return "Success";
        }
        return "Error";
    }
}
